﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Lk.WebTow.Common;
using Lk.WebTow.Common.Autribute;
using Lk.WebTow.Data;
using Lk.WebTow.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Lk.WebTow.Areas.Admin.Controllers
{
    public class ManagerController : BaseController
    {

        public ManagerController(ApplicationDbContext dbContext) : base(dbContext)
        {
        }

        public IActionResult Index()
        {
            return View();
        }

        #region 部门管理

        [NavigationAutribute(NavName = "sysDeptuser")]
        public IActionResult DeptManager()
        {
            var list = dbContext.NcManagerDept.ToList();

            List<DeptManagerViewModel> result = list.Select(s => new DeptManagerViewModel() { id = s.DeptId, name = s.DeptName, pid = s.ParentId.Value }).ToList();
            ViewBag.initJson = JsonHelper.ObjectToJSON(result);
            return View();
        }
        #endregion

        #region 角色管理

        public ActionResult Managers()
        {
            int deptId = Utils.StrToInt(Request.Query["deptId"]);//部门id
            int rid = Utils.StrToInt(Request.Query["rid"]);//角色id
            string keyword = Request.Query["keyword"];//搜索关键字

            var count = dbContext.NcManager.Count();
            ViewBag.TotalCount = count;
            ViewBag.RId = rid;
            ViewBag.DeptId = deptId;
            ViewBag.Keyword = keyword;

            var roles = dbContext.NcManagerRole.Where(s => s.Id > 1).ToList();

            ViewBag.roles = roles;
            return View();
        }
        /// <summary>
        /// 管理员列表
        /// </summary>
        [HttpPost]
        [NavigationAutribute(NavName = "sysDeptuser")]
        public string ManagerList()
        {
            var pageSize = Utils.StrToInt(Request.Query["pageSize"]);
            var pageIndex = Utils.StrToInt(Request.Query["pageIndex"]);

            var deptId = Utils.StrToInt(Request.Query["deptId"]);
            var rId = Utils.StrToInt(Request.Query["pageIndex"]);
            var keyword = Request.Query["keyword"];

            string strWhere = Request.Query["sw"].ToString() ?? string.Empty;

            var managers = dbContext.NcManager.Join(dbContext.NcManagerRole, pet => pet.RoleId, per => per.Id, (pet, per) => new NcManagerViewModel
            {
                Avatar = pet.Avatar,
                Id = pet.Id,
                AddTime = pet.AddTime,
                DeptId = pet.DeptId,
                Email = pet.Email,
                IsAudit = pet.IsAudit,
                IsLock = pet.IsLock,
                Password = pet.Password,
                RealName = pet.RealName,
                RoleId = pet.RoleId,
                RoleName = per.RoleName,
                RoleType = pet.RoleType,
                Salt = pet.Salt,
                Telephone = pet.Telephone,
                UserName = pet.UserName,

            });
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                managers = managers.Where(s => keyword.Contains(s.RealName));
            }
            if (rId > 0)
            {
                managers = managers.Where(s => s.RoleId == rId);
            }
            if (deptId > 0)
            {
                managers= managers.Where(s => s.DeptId == deptId);
            }
            //.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
            var result = PaginatedList<NcManagerViewModel>.CreateAsync(managers.AsNoTracking(), pageIndex, pageSize).Result;
            //  int recordCount = dbContext.NcManager.Join(dbContext.NcManagerRole, pet => pet.Id, per => per.Id, (pet, per) => new { id = pet.Id }).Count();
            return JsonHelper.ObjectToJSON(result);
        }
        #endregion
    }
}